package test;

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;

import java.util.*;

import java.io.*;

public class JTableJList //extends JFrame
{
	Vector data;
	Vector cols;
	
	public void init() {
		data = new Vector();
		Vector tmp = new Vector();
		
		String[] sdata = {"test1", "test2", "test3", "test4"};
		tmp.addElement("one");
		tmp.addElement("two");
		data.addElement(tmp);
		
		tmp = new Vector();
		String[] sdata2 = {"stock1", "stock2", "stock3", "stock4"};
		tmp.addElement("1");
		tmp.addElement("2");
		data.addElement(tmp);
		
		cols = new Vector();
		cols.addElement("col1");
		cols.addElement("col2");
		
		
		
		//JTable table = new JTable(data, cols);
		JTable table = new JTable (
			new AbstractTableModel() {
			    public String getColumnName(int col) {
			        return cols.get(col).toString();
			    }
			    public int getRowCount() { return data.size(); }
			    public int getColumnCount() { return cols.size(); }
			    public Object getValueAt(int row, int col) {
			        return ((Vector)data.get(row)).get(col);
			    }
			    public boolean isCellEditable(int row, int col)
			        { return false; }
			    public void setValueAt(Object value, int row, int col) {
//			        rowData[row][col] = value;
//			        fireTableCellUpdated(row, col);
			    }
			}
		);
//		table.
//		DefaultListSelectionModel a = null;
//		a.setAnchorSelectionIndex(0);
//		
		table.setSelectionModel(new DefaultListSelectionModel() {
//			public void setAnchorSelectionIndex(int a) {
//				
//			}
//			
//			public void setLeadSelectionIndex(int a) {
//			}
//			
//			public void setSelectionMode(int selectionMode) {
//			}
//			public void addSelectionInterval(int index0, int index1) {
//				
//			} 
			
//			public int getAnchorSelectionIndex() {
//				return 1;
//			}
			
			public int getLeadSelectionIndex() {
				return -1;
			}
		});
		//		table.setCellSelectionEnabled(true);
//		table.setColumnSelectionAllowed(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//		table.setGridColor(new Color(0,0,0,0));
//		table.setRowSelectionAllowed(true);
		try {
			table.setDefaultRenderer(Class.forName("java.lang.Object"), new DefaultTableCellRenderer() );
		} catch(ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		table.selectAll();
		table.getSelectionModel().setLeadSelectionIndex(6);
		
		table.setAutoCreateRowSorter(true);
		
		TableColumn column = table.getColumnModel().getColumn(0);
		MyListEditor area = new MyListEditor();
		// ROWHIGHT=table.getRowHeight() +table.getRowMargin()+1;
//		column.setCellEditor(area);
		
//		column.setCellRenderer(new MyCellRenderer());
		
		JFrame frame = new JFrame();
		frame.getContentPane().add(new JScrollPane(table));
		frame.pack();
		frame.setVisible(true);
	}
	
	private class MyListEditor extends AbstractCellEditor implements TableCellEditor {
		JList mlist;
		
		public MyListEditor() {
			mlist = new JList();
		}
		
		public Object[] getCellEditorValue() {
			return ((DefaultListModel)mlist.getModel()).toArray();
		}
		
		// This method is called when a cell value is edited by the user.
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
			mlist.setListData((Object[])value);
			return mlist;
		}
		
	}
	
	private class MyCellRenderer extends JList implements TableCellRenderer {
		public MyCellRenderer() {
			super();
		}
		
public Component getTableCellRendererComponent(JTable mtable, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
			
			if(isSelected) setBackground(mtable.getSelectionBackground());
			else setBackground(Color.white);
			String stmp;
			StringTokenizer st;
			int rowHight = mtable.getRowHeight() + mtable.getRowMargin() + 1;
			int fold;
			
			String[] mlist = (String[])value;
			
			fold = mlist.length;
			
			//adjust row height
			if(mtable.getRowHeight(rowIndex) != fold * rowHight) //otherwise, the save dialog couldn't show normally
			mtable.setRowHeight(rowIndex, fold * rowHight);
			
			setListData((String[])value);
			
			return this;
		}
	}
	
	public static void main(String[] args) {
		JTableJList jj = new JTableJList();
		jj.init();
	}
	
}
